package cn.itcast.web.controller.cargo;

import cn.hutool.core.util.StrUtil;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.ExtCproductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.util.ImageUploadUtils;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/cargo/extCproduct", name = "附件管理")
public class ExtCProductController extends BaseController {

    @Reference
    private ExtCproductService extCproductService;

    @Reference
    private FactoryService factoryService;

    @RequestMapping(value = "/list", name = "附件分页查询")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize,
                       String contractId,
                       String contractProductId) {

        // b附件分页对象
        ExtCproductExample extCproductExample = new ExtCproductExample();
        extCproductExample.createCriteria().andContractProductIdEqualTo(contractProductId);
        PageInfo page = extCproductService.findByPage(pageNum, pageSize, extCproductExample);
        request.setAttribute("page", page);

        // b查询货物生产厂家
        // 1.创建厂家查询对象
        FactoryExample factoryExample = new FactoryExample();
        // 2.创建厂家条件对象
        FactoryExample.Criteria factoryExampleCriteria = factoryExample.createCriteria();
        // 3.指定厂家类型条件
        factoryExampleCriteria.andCtypeEqualTo("附件");
        // 5.查询厂家列表
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList", factoryList);

        // c合同id
        request.setAttribute("contractId", contractId);

        // d货物id
        request.setAttribute("contractProductId", contractProductId);
        return "cargo/extc/extc-list";
    }

    @RequestMapping(value = "/edit", name = "新增或修改")
    public String edit(ExtCproduct extCproduct, MultipartFile productPhoto) throws Exception {
        // 图片上传 start
        // 非空判断
        if (productPhoto != null && !productPhoto.isEmpty()) {
            // 调用七牛云工具类实现文件上传
            String imgUrl = ImageUploadUtils.upload(productPhoto.getInputStream());
            extCproduct.setProductImage(imgUrl);
        }
        // 图片上传 end

        if (StrUtil.hasEmpty(extCproduct.getId())) {
            // 保存附件
            extCproduct.setCompanyId(getCompanyId());
            extCproduct.setCompanyName(getCompanyName());
            extCproduct.setCreateBy(getLoginUser().getId());
            extCproduct.setCreateDept(getLoginUser().getDeptId());
            extCproduct.setCreateTime(new Date());
            extCproductService.save(extCproduct);
        } else {
            // 更新附件
            extCproduct.setUpdateBy(getLoginUser().getId());
            extCproduct.setUpdateTime(new Date());
            extCproductService.update(extCproduct);
        }

        return "redirect:/cargo/extCproduct/list.do?contractId=" + extCproduct.getContractId() + "&contractProductId=" + extCproduct.getContractProductId();
    }

    @RequestMapping(value = "/toUpdate", name = "去修改页面")
    public String toUpdate(String id) {
        // a根据附件id，查询附件对象
        ExtCproduct extCproduct = extCproductService.findById(id);
        request.setAttribute("extCproduct", extCproduct);

        // b查询货物生产厂家
        // 1.创建厂家查询对象
        FactoryExample factoryExample = new FactoryExample();
        // 2.创建厂家条件对象
        FactoryExample.Criteria factoryExampleCriteria = factoryExample.createCriteria();
        // 3.指定厂家类型条件
        factoryExampleCriteria.andCtypeEqualTo("附件");
        // 5.查询厂家列表
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList", factoryList);
        return "cargo/extc/extc-update";
    }

    @RequestMapping(value = "/delete", name = "删除")
    public String delete(String id, String contractId, String contractProductId) {
        extCproductService.delete(id);
        return "redirect:/cargo/extCproduct/list.do?contractId=" + contractId + "&contractProductId=" + contractProductId;
    }
}
